package com.gm.wholesale.module.aftersale.view;

import android.content.Context;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.filters.RegexConstant;
import com.gm.commonlib.filters.RegexResultFilter;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.commonlib.views.toast.ToastUtil;
import com.gm.wholesale.R;
import com.gm.wholesale.module.order.bean.OrderDetailBean;

public class AftersaleListItem extends LinearLayout {
    private TextView goodNameTv;
    private TextView goodNumTv;
    private TextView goodUnitTv;
    private TextView goodPriceTv;
    public EditText afterSaleNumEd;

    public AftersaleListItem(Context context) {
        super(context);
        initView(context);
    }

    public AftersaleListItem(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.list_aftersale_item, this);
        goodNameTv = findViewById(R.id.goodNameTv);
        goodNumTv = findViewById(R.id.goodNumTv);
        goodUnitTv = findViewById(R.id.goodUnitTv);
        goodPriceTv = findViewById(R.id.goodPriceTv);
        afterSaleNumEd = findViewById(R.id.afterSaleNumView);
        afterSaleNumEd.setFilters(new InputFilter[]{new RegexResultFilter(RegexConstant.MAX_MONEY_VALUE_92)});

    }
    public OrderDetailBean.OrderDetailsDTO mData;
    public void showView(OrderDetailBean.OrderDetailsDTO detailBean){
        if (detailBean==null){
            return;
        }
        mData=detailBean;
        goodNameTv.setText(detailBean.getSkuName());
        goodNumTv.setText(detailBean.getQuantity()+"");
        goodUnitTv.setText(detailBean.getOutgoingUnitName());
        goodPriceTv.setText(NumberUtils.getMoneyString(Double.parseDouble(detailBean.getUnitPrice())));
    }
    public OrderDetailBean.OrderDetailsDTO getInputData(){
        String inputDatas=afterSaleNumEd.getText().toString();
        if (!TextUtils.isEmpty(inputDatas)){
            double inputData=Double.parseDouble(inputDatas);
            if (mData.getQuantity()==null){
                return mData;
            }
            if (inputData>Double.parseDouble(mData.getQuantity())){
                ToastUtil.showToast("商品【"+mData.getSkuName()+"】退款数量不能大于购买数量");
                return null;
            }
            mData.setAfterNum(inputData);
        }
        return mData;
    }

    public double getInputNumberData(){
        String data=afterSaleNumEd.getText().toString();
        if (TextUtils.isEmpty(data)){
            return 0;
        }
        return Double.parseDouble(data);
    }
}
